<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="utf-8">
    <title>模板列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" href="../../../layuiadmin/layui/css/layui.css"
          media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" href="../../../layuiadmin/style/admin.css"
          media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label for="authorityName" class="layui-form-label">资源名称:</label>
                    <div class="layui-input-block">
                        <input type="text" id="authorityName" name="authorityName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label for="authorityController" class="layui-form-label">控制器:</label>
                    <div class="layui-input-block">
                        <input type="text" id="authorityController" name="authorityController" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label for="authorityAction" class="layui-form-label">Action:</label>
                    <div class="layui-input-block">
                        <input type="text" id="authorityAction" name="authorityAction" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>

                </div>
                <div class="layui-inline">
                    <label for="authorityType" class="layui-form-label">资源类型:</label>
                    <div class="layui-input-inline">
                        <input type="text" id="authorityType" name="authorityType" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="lay-table-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin layui-hide" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
            </div>

            <table id="lay-table" lay-filter="lay-table"></table>
<!--            <script type="text/html" id="imgTpl">-->
<!--                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>-->
<!--            </script>-->
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        //表格实例
        table.render({
            elem: '#lay-table'
            , url: '/system/resource/layGrid' //数据接口
            , page: true //开启分页
            , limit: 10
            , cols: [
                [
                    {type: 'numbers', title: '序号', fixed: 'left'},
                    {field: 'resourceName', title: '名称'},
                    {field: 'resourceDesc', title: '描述'},
                    {
                        field: 'deleted', title: '是否删除', templet: function (d) {
                            return d === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'createTime ', title: '添加日期', templet: function (d) {
                            return layui.util.toDateString(d.createTime, "yyyy-MM-dd");
                        }
                    },
                    {title: "操作", templet: "#table_op", width: 220}
                ]]
        });

        //监听搜索
        form.on('submit(lay-table-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('lay-table', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('lay-table')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);

                    layer.confirm('确定删除吗？', function (index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        layer.close(index);
                        table.reload('lay-table');
                        layer.msg('已删除');

                    });
                });
            }
            , add: function () {
                var indexAdd = layer.open({
                    type: 2
                    , title: '添加用户'
                    , content: 'add.html'
                    , maxmin: true
                    , area: ['80%', '80%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var windowSubmitBtn = $('#layui-layer' + index + " a.layui-layer-btn0");
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'lay-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data){
                            iframeWindow.$("#myForm").ajaxSubmit({
                                type: 'post',
                                beforeSubmit: function () {
                                    layer.msg('数据提交中...', {time: 0});
                                    //console.log(iframeWindow);
                                    //$('#layui-layer' + index + " a.layui-layer-btn0").addClass("layui-btn layui-btn-disabled");
                                    //隐藏提交按钮（超链接），防止重复提交
                                    windowSubmitBtn.hide();
                                },
                                success: function (data) {
                                    if (data.code === 0) {
                                        layer.close(indexAdd);
                                        layer.msg('保存成功');
                                        table.reload('lay-table');
                                    } else {
                                        layer.alert('添加失败');
                                        windowSubmitBtn.show();
                                    }
                                },
                                error: function (err) {
                                    layer.alert(err.msg);
                                    windowSubmitBtn.show();
                                }

                            });
                        })
                        submit.trigger('click');

                    }
                });
                //layer.full(indexAdd);
            }
        };


        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

<!--模板(操作)-->
<script type="text/html" id="table_op">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view"><i
            class="layui-icon layui-icon-about"></i>查看</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
</body>
</html>
